package account;

import utils.DBConnection;
import utils.TextUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.List;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Shawn
 * Date: 2009-5-28
 * Time: 17:16:29
 */
public class Account {

  public static int addAccount(Connection con, AccountInfo info) throws SQLException {
    String sql = "INSERT INTO accounts VALUES (NULL, ?, ?, ?, ?, ?, ?)";
    PreparedStatement ps = con.prepareStatement(sql);
    ps.setString(1, info.getAname());
    ps.setString(2, info.getAccount());
    ps.setString(3, info.getPassword());
    ps.setString(4, info.getGroup());
    ps.setString(5, info.getLevel());
    ps.setLong(6, info.getRights());
    ps.executeUpdate();
    return DBConnection.getLastID(con);
  }

  public static void modiAccount(Connection con, AccountInfo info) throws SQLException {
    String sql = "UPDATE accounts SET aname=?, account=?, password=?, groupName=?, level=?, rights=? WHERE aid=? LIMIT 1";
    PreparedStatement ps = con.prepareStatement(sql);
    ps.setString(1, info.getAname());
    ps.setString(2, info.getAccount());
    ps.setString(3, info.getPassword());
    ps.setString(4, info.getGroup());
    ps.setString(5, info.getLevel());
    ps.setLong(6, info.getRights());
    ps.setInt(7, info.getAid());
    ps.executeUpdate();
  }

  public static void delAccount(Connection con, int aid) throws SQLException {
    String sql = "DELETE FROM accounts WHERE aid=" + aid + " LIMIT 1";
    con.createStatement().executeUpdate(sql);
  }

  public static AccountInfo getAccountInfo(Connection con, int aid) throws SQLException {
    String sql = "SELECT * FROM accounts WHERE aid=" + aid + " LIMIT 1";
    ResultSet rs = con.createStatement().executeQuery(sql);
    return rs.next() ? rs2Info(rs) : new AccountInfo();
  }

  public static AccountInfo getAccountInfo(Connection con, String account, String password) throws Exception {
    String sql = "SELECT * FROM accounts WHERE account=? AND password=? LIMIT 1";
    PreparedStatement ps = con.prepareStatement(sql);
    ps.setString(1, account);
    ps.setString(2, TextUtils.SHAString(password));
    ResultSet rs = ps.executeQuery();
    return rs.next() ? rs2Info(rs) : new AccountInfo();
  }

  public static List<AccountInfo> getAccountList(Connection con) throws SQLException {
    String sql = "SELECT * FROM accounts";
    ResultSet rs = con.createStatement().executeQuery(sql);
    ArrayList<AccountInfo> accounts = new ArrayList<AccountInfo>();
    while (rs.next()) accounts.add(rs2Info(rs));
    return accounts;
  }

  private static AccountInfo rs2Info(ResultSet rs) throws SQLException {
    AccountInfo info = new AccountInfo();
    info.setAid(rs.getInt("aid"));
    info.setAname(rs.getString("aname"));
    info.setAccount(rs.getString("account"));
    info.setPassword(rs.getString("password"));
    info.setGroup(rs.getString("groupName"));
    info.setLevel(rs.getString("level"));
    info.setRights(rs.getInt("rights"));
    return info;
  }
}
